package android.slc.appbase.ui.vd;

import android.os.Bundle;
import android.slc.adapter.CommonlySwipeRecycler;
import android.slc.appbase.R;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.io.Serializable;
import java.util.List;

/**
 * 基础的列表盒子vm
 *
 * @author slc
 * @date 2020-08-25 16:13
 */
public abstract class BaseListCommonVd<T extends Serializable, VB extends BaseListCommonVmBox<T>> extends BaseDataChangeVd<VB> {
    protected CommonlySwipeRecycler<T> swipeRecycler;
    protected ActivityResultLauncher<Bundle> itemClickActivityResultLauncher;

    protected void initView() {
        super.initView();
        initSwipeRecycler();
    }

    /**
     * 遇到数据加载失败但是List<>有数据时多半是此处的问题，根据问题所在重写该方法即可
     */
    protected void initSwipeRecycler() {
        swipeRecycler = perfectRecyclerBuilder()
                .build(this::initAdapterAndBaseEvent);
    }

    /**
     * 完善RecyclerBuilder参数
     *
     * @return
     */
    protected CommonlySwipeRecycler.CommonlySwipeRecyclerBuilder<T> perfectRecyclerBuilder() {
        return new CommonlySwipeRecycler.CommonlySwipeRecyclerBuilder<T>(contentView)
                .setOnLoadAdapterLaterListener(this::onLoadAdapterLaterListener)
                .setSwipeRefreshListener(() -> vmBox.refresh());
    }


    protected BaseQuickAdapter<T, BaseViewHolder> initAdapterAndBaseEvent() {
        BaseQuickAdapter<T, BaseViewHolder> reportByAdapter = getAdapter(vmBox.getDataList());
        reportByAdapter.setOnItemClickListener((adapter, view, position) -> onAdapterItemClick(reportByAdapter, view, position));
        return reportByAdapter;
    }

    protected void onLoadAdapterLaterListener(BaseQuickAdapter<T, BaseViewHolder> adapter){
        adapter.setEmptyView(R.layout.app_data_is_empty);
    }

    protected void onAdapterItemClick(@NonNull BaseQuickAdapter<T, BaseViewHolder> adapter, @NonNull View view, int position) {
        vmBox.itemClick(position, adapter.getItem(position));
    }

    protected abstract BaseQuickAdapter<T, BaseViewHolder> getAdapter(List<T> list);

    protected void initMustCondition() {
        super.initMustCondition();
        if (this.vmBox.getGotoTargetActivityItw() != null) {
            this.itemClickActivityResultLauncher = this.activityResultCaller.registerForActivityResult(this.vmBox.actionRegisterActivityFunc.getTargetActivityResultContract(),
                    this.vmBox.actionRegisterActivityFunc.getOnTargetActivityResultCallback());
        }
        this.vmBox.showItemClickUiOf.observe((LifecycleOwner) activityResultCaller, this::onShowItemClickUiAction);
    }

    protected void onShowItemClickUiAction(Bundle bundle){
        if (itemClickActivityResultLauncher != null) {
            itemClickActivityResultLauncher.launch(bundle);
        }
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        super.onDestroy(owner);
        swipeRecycler.getRecyclerView().setAdapter(null);
    }
}
